package flc.ast.fragment;

import static android.app.Activity.RESULT_OK;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.AudioExtractActivity;
import flc.ast.activity.AudioFormatActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.UriUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private int REQ_SEL_AUDIO = 1001;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rlVideoAudio.setOnClickListener(this);
        mDataBinding.rlAudioConvert.setOnClickListener(this);
        mDataBinding.rlVideoConvert.setOnClickListener(this);
        mDataBinding.ivVideoGif.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlVideoAudio://音频提取
                SelVideoActivity.type = 0;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.rlAudioConvert://音频格式转换
                getPermission();
                break;
            case R.id.rlVideoConvert://视频格式转换
                SelVideoActivity.type = 1;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivVideoGif://视频转GIF
                SelVideoActivity.type = 2;
                startActivity(SelVideoActivity.class);
                break;

        }
    }

    @Override
    protected void initData() {

    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_AUDIO).reqPermissionDesc(getString(R.string.get_audio_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                IntentUtil.pickAudio(HomeFragment.this, REQ_SEL_AUDIO);
            }
        }).request();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQ_SEL_AUDIO) {
            Uri audioUri = data.getData();
            if (audioUri == null) {
                return;
            }
            String filePath = UriUtil.uri2File(audioUri).getPath();
            AudioFormatActivity.audioPath = filePath;
            startActivity(AudioFormatActivity.class);
        }
    }

}